package com.wwwc.index.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.SQLException;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;

import com.wwwc.util.web.*;
import com.wwwc.index.ejb.EJBManager;
import com.wwwc.index.ejb.admin.*;
import com.wwwc.index.ejb.language.*;
import com.wwwc.index.ejb.category.*;
import com.wwwc.index.ejb.directory.*;

public class AdminServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response) {
	
	try {
	    response.setContentType("text/html");

	    String user_ip       = request.getRemoteAddr();
	    HttpSession session  = request.getSession(true);
	    PrintWriter out      = response.getWriter();
	    String call_path     = request.getContextPath()+request.getServletPath();

	    UserProfile user_profile = (UserProfile)session.getAttribute("UserProfile");

	    if(user_profile == null || user_profile.getUserLevel()<99){
		System.out.println("AdminServlet:UserLevel<99");
		return;
	    }

	    String lid = (String)session.getAttribute("SelectedLanguageId");
	    if(lid == null) {
		System.out.println("DirectoryServlet:Error:lid==null");
		return;
	    }

	    Hashtable mhs = (Hashtable)session.getAttribute("LanguageHash_"+lid);
	    if(mhs == null) {
		System.out.println("DirectoryServlet:Error:LanguageHash==null");
		return;
	    }

	    String table_name = request.getParameter("table_name");

	    if(table_name == null) {
		System.out.println("AdminServlet:Error:table_name == null.");
		return;
	    }

	    DirectoryDetails dd = (DirectoryDetails)session.getAttribute("LastDirectoryDetails_"+lid);
	    if(dd == null) {
		System.out.println("AdminServlet:Error:DirectoryDetails not found.");
		return;
	    }

	    String admin_what = dd.getName();
	    String action = request.getParameter("aid");

	    if(admin_what == null) {
		return;
	    }

	    //////////////////////////////////////////////////////////////////////////////////////////////////
	    // Language
	    //////////////////////////////////////////////////////////////////////////////////////////////////

	    if(admin_what.equals("Language")) {
		Language ejbdb = (Language)EJBManager.getEJB("java:comp/env/ejb/LanguageEJB", "Language");

		//////////////////////////////////////////////////
		// Translation
		//////////////////////////////////////////////////

		if(action != null && action.equals("9-1-1")) {
		 
		    String name = request.getParameter("fid");

		    String tran_value = request.getParameter("tran_name");
		    String tran_key = request.getParameter("key");
		    String submit = request.getParameter("tran_update");
			
		    if(submit != null) {
			if(tran_value == null || tran_value.length()==0) {
			    out.println("Error.");
			}
			else {
			    ejbdb.translateLanguage(name, tran_key, tran_value);
			    System.out.println("key="+tran_key);
			    System.out.println("value="+tran_value);
			}
		    }

		    ArrayList aa = ejbdb.getLanguageTransArrayList(name);
		    if(aa == null || aa.isEmpty()) {
			out.println("<BR><CENTER>Error. Language ArrayList is empty. Please try again!</CENTER>");
			return;
		    }
			
		    out.println("<BR><TABLE align=center cellSpacing='0' cellPadding='0' border='1' width=90%>");
		    out.println("<TR><TD align=center colspan=5><B>Translate English to "+name+"</B></TD></TR>");
		    out.println("<TR><TD align=center><B>Key</B></TD>");
		    out.println("<TD align=center><B>English</B></TD>");
		    out.println("<TD align=center><B>"+name+"</B></TD>");
		    out.println("<TD align=center colspan=2><B>Update</B></TD></TR>");
			
		    Iterator it = aa.iterator();
		    ArrayList a = null;
		    
		    while(it.hasNext()) {
			a = (ArrayList)it.next();
			out.println("<FORM action="+call_path+" method=post>");
			out.println("<TR><TD align=center>"+a.get(0)+"</TD>");
			out.println("<TD align=center>"+a.get(1)+"</TD>");
			out.println("<TD align=center>"+a.get(2)+"</TD>");
			out.println("<TD align=left>");
			out.println("<input type=text name=tran_name value='"+a.get(2)+"' size=50  maxLength=250></TD>");
			out.println("<TD align=center><input type=submit name=tran_update value=Update></TD></TR>");
			out.println("<input type=hidden name=aid value=9-1-1>");
			out.println("<input type=hidden name=fid value="+name+">");
			out.println("<input type=hidden name=key value='"+a.get(0)+"'>");
			out.println("</FORM>");
		    }
		    out.println("</TABLE>");
		}

		//////////////////////////////////////////////////
		// Set Default
		//////////////////////////////////////////////////

		if(action != null && action.equals("9-1-2")) {
		    String name = request.getParameter("fid");  
		    ejbdb.setDefaultLanguage(name);
		    session.setAttribute("SelectedLanguageId", null);
		    out.println("<CENTER><BR>Default language has been changed to:"+ name+".</CENTER>");
		}

		//////////////////////////////////////////////////
		// Add support
		//////////////////////////////////////////////////

		if(action != null && action.equals("9-1-3")) {
		    String name = request.getParameter("fid");
		    if(ejbdb.addLanguageSupport(name) >0) {
			out.println("<CENTER><BR>"+name+" has been added.</CENTER>");
		    }
		}

		//////////////////////////////////////////////////
		// Remove
		//////////////////////////////////////////////////

		if(action != null && action.equals("9-1-4")) {
		    String name = request.getParameter("fid");
		    if(ejbdb.removeLanguageSupport(name) == 1) {
			out.println("<CENTER><BR>"+name+" has been removed.</CENTER>");
		    }
		}

		//////////////////////////////////////////////////
		// Default 
		//////////////////////////////////////////////////

		ArrayList aa = ejbdb.getAllLanguageList();
		out.println("<BR>");
		out.println("<TABLE align=center cellSpacing='0' cellPadding='0' border='1' width=90%>");
		out.println("<TR><TD align=center><B>Language</B></TD>");
		out.println("<TD align=center><B>Abbreviation</B></TD>");
		out.println("<TD align=center><B>Translate/Edit</B></TD>");
		out.println("<TD align=center><B>Add/Remove</B></TD>");
		out.println("<TD align=center><B>Default</B></TD>");
		out.println("</TR>");

		Iterator it = aa.iterator();
		ArrayList a = null;
		int stage = 0;
		
		while(it.hasNext()) {
		    a = (ArrayList)it.next();
		    stage = Integer.parseInt((String)a.get(2));
		    out.println("<TR>");
		    out.println("<TD align=center>"+a.get(0)+"</TD>");
		    out.println("<TD align=center>"+a.get(1)+"</TD>");
		    
		    if(stage == 10){
			out.println("<TD align=center><A href="+call_path+"?aid=9-1-1&fid="+a.get(0)+">Edit</A></TD>");
			out.println("<TD align=center>Default</TD>");
			out.println("<TD align=center>Default</TD>");
		    }
		    else if(stage > 10){
			out.println("<TD align=center><A href="+call_path+"?aid=9-1-1&fid="+a.get(0)+">Edit</A></TD>");
			out.println("<TD align=center><A href="+call_path+"?aid=9-1-4&fid="+a.get(0)+">Remove</A></TD>");
			out.println("<TD align=center><A href="+call_path+"?aid=9-1-2&fid="+a.get(0)+">Set Default</A></TD>");
		    }
		    else {
			out.println("<TD align=center><A href="+call_path+"?aid=9-1-1&fid="+a.get(0)+">Translate</A></TD>");
			out.println("<TD align=center><A href="+call_path+"?aid=9-1-3&fid="+a.get(0)+">Add</A></TD>");
			out.println("<TD align=center>N/A</TD>");
		    }

		    out.println("</TR>");
		}
		out.println("</TABLE>");
		return;
	    }

	    //////////////////////////////////////////////////////////////////////////////////////////////////
	    // Category
	    //////////////////////////////////////////////////////////////////////////////////////////////////

	    if(admin_what.equals("Category")) {
		String error = null;
		String category_id = request.getParameter("fid");
		String category_name  = request.getParameter("category_name");
		String category_level = request.getParameter("category_level");
		
		if(category_name == null) {
		    category_name = "";
		}
		else {
		    if(category_name.length()==0) {
			error = "Category Name is required.";
		    }
		}

		if(category_level == null) {
		    category_level = "";
		}
		else {
		    if(error == null) {
			if(!UserInputCheck.isNumber(category_level)) {
			    error = "Access Level should be a number between 0-99";
			}
			else {
			    if(Integer.parseInt(category_level)>99 ||Integer.parseInt(category_level)<0) {
				error = "Access Level should be a number between 0-99";
			    }
			}
		    }
		}

		Category ejbdb = (Category)EJBManager.getEJB("java:comp/env/ejb/CategoryEJB", "Category");

		if(action != null && action.equals("Default")) {
		    if(error == null) {
			if(ejbdb.setDefaultCategory(category_id)==0) {
			    error = "Set Default Category Error!";
			}
		    }

		    category_name  = "";
		    category_level = "";
		}

		if(action != null && action.equals("Update")) {
		    if(error == null) {
			if(ejbdb.updateCategory(category_id, category_name, category_level) ==0) {
			    error = "Update category:"+category_name+" error!";
			}
		    }

		    category_name  = "";
		    category_level = "";
		}

		if(action != null && action.equals("Delete")) {
		    if(error == null) {
			if(ejbdb.deleteCategory(category_id) !=1) {
			    error = "Delete category:"+category_name+" error!";
			}
		    }
		    category_name  = "";
		    category_level = "";
		}

		if(action != null && action.equals("Add New Category")) {
		    if(error == null) {
			if(ejbdb.addNewCategory(category_id, category_name, category_level)==1) {
			    out.println("<center>");
			    out.println("<BR>Category:"+category_name+" has been added.");
			    out.println("</center>");
			    category_name = "";
			    category_level= "";
			}
			else {
			    error = "Adding new category error.";
			}
		    }
		}

		ArrayList ca = ejbdb.getCategoryList(99);
		Iterator it = ca.iterator();

		out.println("<BR>");
		out.println("<CENTER><B>Category Admin</B></CENTER><BR>");
		out.println("<TABLE width=90% align=center border=1 cellSpacing=0 cellPadding=0>");
		if(error != null && error.length()>0) {
		    out.println("<TR>");
		    out.println("<TD align=center colspan=6><img src=/images/star.gif>"+error+"</TD>");
		    out.println("</TR>");
		}
		out.println("<TR>");
		out.println("<TD align=center><B>Categroy ID</B></TD>");
		out.println("<TD align=center><B>Category Name</B></TD>");
		out.println("<TD align=center><B>Access Level</B></TD>");
		out.println("<TD align=center><B>Default</B></TD>");
		out.println("<TD align=center><B>Update</B></TD>");
		out.println("<TD align=center><B>Delete</B></TD>");
		out.println("</TR>");

		CategoryDetails cd = null;

		while(it.hasNext()) {
		    cd = (CategoryDetails)it.next();
		    if(cd.getCategoryId()==0) {
			continue;
		    }
		    out.println("<FORM method=post action="+call_path+">");
		    out.println("<TR>");
		    out.println("<TD align=center>"+cd.getCategoryId()+"</TD>");
		    out.println("<TD align=center>");
		    out.println("<input type=text name=category_name value=\""+cd.getCategoryName()+"\" size=20 maxLength=20></TD>");
		    out.println("<TD align=center>");
		    out.println("<input type=text name=category_level value=\""+cd.getCategoryLevel()+"\" size=4 maxLength=5></TD>");
		  
		    if(cd.getCategoryId() == 10) {
			out.println("<TD align=center>Default</TD>");
		    }
		    else {
			out.println("<TD align=center>");
			out.println("<input type=submit name=aid value=Default style='font-size: 8pt'></TD>");
		    }
		    out.println("<TD align=center><input type=submit name=aid value=Update style='font-size: 8pt'></TD>");
		    out.println("<TD align=center><input type=submit name=aid value=Delete style='font-size: 8pt'></TD>");
		    out.println("</TR>");
		    out.println("<input type=hidden name=fid value="+cd.getCategoryId()+">");
		    out.println("</FORM>");
		}

		out.println("<FORM method=post action="+call_path+">");
		out.println("<TR>");
		out.println("<TD align=center>"+(cd.getCategoryId()+10)+"</TD>");
		out.println("<TD align=center>");
		out.println("<input type=text name=category_name value=\""+category_name+"\" size=20 maxLength=20></TD>");
		out.println("<TD align=center>");
		out.println("<input type=text name=category_level value=\""+category_level+"\" size=4 maxLength=5></TD>");
		out.println("<TD align=center colspan=3>");
		out.println("<input type=submit name=aid value='Add New Category' style='font-size: 8pt'></TD>");
		out.println("</TR>");
		out.println("<input type=hidden name=fid value="+(cd.getCategoryId()+10)+">");
		out.println("</FORM>");
		out.println("</TABLE>");
		return;
	    }

	    if(admin_what.startsWith("Admin")) {
		CategoryDetails cd = (CategoryDetails)session.getAttribute("SelectedCategory");
		if(cd == null) {
		    System.out.println("AdminServlet:Error:SelectedCategory == null");
		    return;
		}

		if(table_name == null) {
		    out.println("<CENTER><BR>Admin Error.</CENTER>");
		    return;
		}

		out.println("<CENTER><BR>"+dd.getName()+" "+cd.getCategoryName()+"</CENTER>");
		String error = null;

		String dir_id    = null;
		String dir_name  = null;
		String dir_type  = null;
	
		String d_level = null;
		String d_age   = null;
		String p_level = null;
		String p_age   = null;
		String r_level = null;
		String r_age   = null;
		String w_level = null;
		String w_age   = null;

		String managers = null;
		String admins   = null;

		Directory ejbdb = (Directory)EJBManager.getEJB("java:comp/env/ejb/DirectoryEJB", "Directory");
		
		if(action != null && action.equals("Update")) {
		    dir_id    = request.getParameter("dir_id");
		    dir_name  = request.getParameter("dir_name");
		    dir_type  = request.getParameter("dir_type");
		    d_level   = request.getParameter("d_level");
		    d_age     = request.getParameter("d_age");
		    p_level   = request.getParameter("p_level");
		    p_age     = request.getParameter("p_age");
		    r_level   = request.getParameter("r_level");
		    r_age     = request.getParameter("r_age");
		    w_level   = request.getParameter("w_level");
		    w_age     = request.getParameter("w_age");
		    managers  = request.getParameter("managers");
		    admins    = request.getParameter("admins");

		    if(dir_id == null) {
			error = "Error.";
		    }
		    else {
			if(dir_id.equals("1") || dir_id.startsWith("1-")) {
			    if(1==ejbdb.updateDirectoryDetails(table_name, dir_id, managers, admins)) {
				error = "";
			    }
			    else {
				error = "Update ...... Error!";
			    }
			}
			else {
			    if(dir_name == null || dir_name.length()<1) {
				error = "Name is required.";
			    }

			    if(error == null) {
				if(dir_type == null || dir_type.length()==0) {
				    error = "Type is required.";
				}
				else {
				    if(!UserInputCheck.isNumber(dir_type)) {
					error = "Type Error.";
				    }
				}
			    }

			    if(error == null) {
				if(d_level == null || d_level.length()==0) {
				    error = "List level is required.";
				}
				else {
				    if(!UserInputCheck.isNumber(d_level)) {
					error = "List level error.";
				    }
				}
			    }

			    if(error == null) {
				if(d_age == null || d_age.length()==0) {
				    error = "List age is required.";
				}
				else {
				    if(!UserInputCheck.isNumber(d_age)) {
					error = "List age error.";
				    }
				}
			    }

			    if(error == null) {
				if(p_level == null || p_level.length()==0) {
				    error = "Preview level is required.";
				}
				else {
				    if(!UserInputCheck.isNumber(p_level)) {
					error = "Preview level error.";
				    }
				}
			    }

			    if(error == null) {
				if(p_age == null || p_age.length()==0) {
				    error = "Preview age is required.";
				}
				else {
				    if(!UserInputCheck.isNumber(p_age)) {
					error = "Preview age error.";
				    }
				}
			    }

			    if(error == null) {
				if(p_level == null || p_level.length()==0) {
				    error = "Preview level is required.";
				}
				else {
				    if(!UserInputCheck.isNumber(p_level)) {
					error = "Preview level error.";
				    }
				}
			    }

			    if(error == null) {
				if(p_age == null || p_age.length()==0) {
				    error = "Preview age is required.";
				}
				else {
				    if(!UserInputCheck.isNumber(p_age)) {
					error = "Preview age error.";
				    }
				}
			    }

			    if(error == null) {
				if(r_level == null || r_level.length()==0) {
				    error = "Read level is required.";
				}
				else {
				    if(!UserInputCheck.isNumber(r_level)) {
					error = "Read level error.";
				    }
				}
			    }

			    if(error == null) {
				if(r_age == null || r_age.length()==0) {
				    error = "Read age is required.";
				}
				else {
				    if(!UserInputCheck.isNumber(r_age)) {
					error = "Read age error.";
				    }
				}
			    }
			    
			    if(error == null) {
				if(w_level == null || w_level.length()==0) {
				    error = "Write level is required.";
				}
				else {
				    if(!UserInputCheck.isNumber(w_level)) {
					error = "Write level error.";
				    }
				}
			    }

			    if(error == null) {
				if(w_age == null || w_age.length()==0) {
				    error = "Write age is required.";
				}
				else {
				    if(!UserInputCheck.isNumber(w_age)) {
					error = "Write age error.";
				    }
				}
			    }
			    
			    if(error == null) {
				if(1==ejbdb.updateDirectoryDetails(table_name, dir_id, dir_name, 
								   dir_type, d_level, d_age, p_level, p_age, 
								   r_level, r_age, w_level, w_age, managers, admins)) {
				    error = "";
				    session.setAttribute("DirectoryListVector_"+table_name, null);
				    out.println(HtmlUtils.urlReloadScript(call_path));
				}
				else {
				    error = "Update ...... Error!";
				}
			    }
			}
		    }

		}

		
		Vector v = ejbdb.getDirectoryList(table_name, 99);

		out.println("<BR>");
		
		out.println("<TABLE align=center  cellSpacing='0' cellPadding='3' border =1 width=90%>");
		if(error != null && error.length()>0) {
		    out.println("<TR><TD align=center style='font-size: 8pt' colspan=13>");
		    out.println("<img src=/images/star.gif><I>"+error+"</I></TD></TR>");
		}

		out.println("<TR><TD align=center><B>Name</B></TD>");
		out.println("<TD align=center><B>Type</B></TD>");
		out.println("<TD align=center><B>List Level</B></TD>");
		out.println("<TD align=center><B>List Age</B></TD>");
		out.println("<TD align=center><B>Preview Level</B></TD>");
		out.println("<TD align=center><B>Preview Age</B></TD>");
		out.println("<TD align=center><B>Read Level</B></TD>");
		out.println("<TD align=center><B>Read Age</B></TD>");
		out.println("<TD align=center><B>Write Level</B></TD>");
		out.println("<TD align=center><B>Write Age</B></TD>");
		out.println("<TD align=center><B>Managers</B></TD>");
		out.println("<TD align=center><B>Admins</B></TD>");
		out.println("<TD align=center><B>Update</B></TD>");
		out.println("</TR>");

		
		Enumeration enum = v.elements(); 

		while(enum.hasMoreElements()) {
		    dd = (DirectoryDetails)enum.nextElement();
		    managers = dd.getManagers();
		    if(managers == null) {
			managers = "";
		    }

		    admins   = dd.getAdmins();
		    if(admins == null) { 
			admins = "";
		    }

		    dir_id    = dd.getId();
		    dir_name  = dd.getName();
		    dir_type  = ""+dd.getType();
	
		    d_level = ""+dd.getDirectoryLevel();
		    d_age   = ""+dd.getDirectoryAge();
		    p_level = ""+dd.getPreviewLevel();
		    p_age   = ""+dd.getPreviewAge();
		    r_level = ""+dd.getReadLevel();
		    r_age   = ""+dd.getReadAge();
		    w_level = ""+dd.getWriteLevel();
		    w_age   = ""+dd.getWriteAge();

		    if(dd.getLink() != null && dd.getLink().startsWith("/AdminServlet")) {
			out.println("<FORM action="+call_path+" method=post>");
			out.println("<TR><TD align=center>"+dir_name+"</TD>");
			out.println("<TD align=center>"+dir_type+"</TD>");
			out.println("<TD align=center>"+d_level+"</TD>");
			out.println("<TD align=center>"+d_age+"</TD>");
			out.println("<TD align=center>"+p_level+"</TD>");
			out.println("<TD align=center>"+p_age+"</TD>");
			out.println("<TD align=center>"+r_level+"</TD>");
			out.println("<TD align=center>"+r_age+"</TD>");
			out.println("<TD align=center>"+w_level+"</TD>");
			out.println("<TD align=center>"+w_age+"</TD>");

			out.println("<TD align=center>");
			out.println("<input type=text name=managers value=\""+managers+"\" size=11 style='font-size: 6pt'></TD>");
			out.println("</TD>");

			out.println("<TD align=center>");
			out.println("<input type=text name=admins value=\""+admins+"\" size=11 style='font-size: 6pt'></TD>");
			out.println("</TD>");

			out.println("<TD align=center>");
			out.println("<input type=submit name=aid value='Update' style='font-size: 8pt'></TD>");
			out.println("</TD>");
			out.println("</TR>");
			out.println("<input type=hidden name=dir_id value='"+dir_id+"'>");
			out.println("</FORM>");
		    }
		    else {
			out.println("<FORM action="+call_path+" method=post>");
			out.println("<TR>");
			out.println("<TD align=center>");
			out.println("<input type=text name=dir_name value=\""+dir_name+"\" size=11 style='font-size: 6pt'></TD>");
			out.println("</TD>");

			out.println("<TD align=center>");
			out.println("<input type=text name=dir_type value=\""+dir_type+"\" size=2 style='font-size: 6pt'></TD>");
			out.println("</TD>");

			out.println("<TD align=center>");
			out.println("<input type=text name=d_level value=\""+d_level+"\" size=2 style='font-size: 6pt'></TD>");
			out.println("</TD>");

			out.println("<TD align=center>");
			out.println("<input type=text name=d_age value=\""+d_age+"\" size=2 style='font-size: 6pt'></TD>");
			out.println("</TD>");

			out.println("<TD align=center>");
			out.println("<input type=text name=p_level value=\""+p_level+"\" size=2 style='font-size: 6pt'></TD>");
			out.println("</TD>");

			out.println("<TD align=center>");
			out.println("<input type=text name=p_age value=\""+p_age+"\" size=2 style='font-size: 6pt'></TD>");
			out.println("</TD>");

			out.println("<TD align=center>");
			out.println("<input type=text name=r_level value=\""+r_level+"\" size=2 style='font-size: 6pt'></TD>");
			out.println("</TD>");

			out.println("<TD align=center>");
			out.println("<input type=text name=r_age value=\""+r_age+"\" size=2 style='font-size: 6pt'></TD>");
			out.println("</TD>");

			out.println("<TD align=center>");
			out.println("<input type=text name=w_level value=\""+w_level+"\" size=2 style='font-size: 6pt'></TD>");
			out.println("</TD>");

			out.println("<TD align=center>");
			out.println("<input type=text name=w_age value=\""+w_age+"\" size=2 style='font-size: 6pt'></TD>");
			out.println("</TD>");

			out.println("<TD align=center>");
			out.println("<input type=text name=managers value=\""+managers+"\" size=11 style='font-size: 6pt'></TD>");
			out.println("</TD>");

			out.println("<TD align=center>");
			out.println("<input type=text name=admins value=\""+admins+"\" size=11 style='font-size: 6pt'></TD>");
			out.println("</TD>");

			out.println("<TD align=center>");
			out.println("<input type=submit name=aid value='Update' style='font-size: 8pt'></TD>");
			out.println("</TD>");
			out.println("</TR>");
			out.println("<input type=hidden name=dir_id value='"+dir_id+"'>");
			out.println("</FORM>");
		    }
		}

		out.println("<TR><TD align=center style='font-size: 8pt' colspan=13>");
		out.println("<input type=submit name=aid value='Add New' style='font-size: 8pt'>");
		out.println("</TD></TR>");

		out.println("</TABLE>");

		return;
	    }
	}
	catch (Exception e) {
	    System.out.println("AdminServlet:Exception:"+e);
	}
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException 
    {
	doPost(request, response);
    }
}








